package handler

import (
	"encoding/json"
	"io"
	"quant/backend/api/dto"
	"quant/backend/api/response"
	"quant/backend/constants"

	"github.com/duke-git/lancet/v2/mathutil"
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
)

type TickWrapper struct {
	StockCode string  `json:"stock_code"`
	LastPrice float64 `json:"last_price"`
}
type marketHandler struct {
}

func NewMarketHandler() *marketHandler {
	return &marketHandler{}
}

// Tick
// @Summary Tick
// @Tags 行情
// @Accept json
// @Produce json
// @Description 订阅股票
// @Param code query string true "股票代码" default(300184.SZ)
// @Param SubscribeDto body dto.TickDto true "订阅信息"
// @Success 200 {object} dto.ResponseDto "成功"
// @Failure 500 {object} dto.ResponseDto "失败"
// @Router /market/tick [post]
func (h *marketHandler) Tick(ctx *gin.Context) {
	stockCode := ctx.Query("stock_code")
	if stockCode == "" {
		response.Failure(ctx, "参数错误: stock_code为空")
		return
	}

	bytes, err := io.ReadAll(ctx.Request.Body)
	if err != nil {
		logrus.Error(err)
		response.FailureWithBody(ctx, "读取 Body 错误", err.Error())
		return
	}
	// fmt.Println("bytes:", string(bytes))

	var tickDto dto.TickDto
	if err := json.Unmarshal(bytes, &tickDto); err != nil {
		logrus.Error(err)
		response.FailureWithBody(ctx, "json转 dto 错误", err.Error())
		return
	}
	tickDto.LastClose = mathutil.RoundToFloat(tickDto.LastClose, constants.Precision2)
	tickDto.Open = mathutil.RoundToFloat(tickDto.Open, constants.Precision2)
	tickDto.High = mathutil.RoundToFloat(tickDto.High, constants.Precision2)
	tickDto.Low = mathutil.RoundToFloat(tickDto.Low, constants.Precision2)
	tickDto.LastPrice = mathutil.RoundToFloat(tickDto.LastPrice, constants.Precision2)
	tickDto.Pvolume = mathutil.RoundToFloat(tickDto.Pvolume, constants.Precision2)
	tickDto.Volume = mathutil.RoundToFloat(tickDto.Volume, constants.Precision2)
	tickDto.Amount = mathutil.RoundToFloat(tickDto.Amount, constants.Precision2)
	tickDto.Speed1Min = mathutil.RoundToFloat(tickDto.Speed1Min, constants.Precision2)
	tickDto.Speed5Min = mathutil.RoundToFloat(tickDto.Speed5Min, constants.Precision2)
	tickDto.VolRatio = mathutil.RoundToFloat(tickDto.VolRatio, constants.Precision2)
	tickDto.PE = mathutil.RoundToFloat(tickDto.PE, constants.Precision2)
	tickDto.LastSettlementPrice = mathutil.RoundToFloat(tickDto.LastSettlementPrice, constants.Precision2)
	tickDto.SettlementPrice = mathutil.RoundToFloat(tickDto.SettlementPrice, constants.Precision2)

	for idx, ap := range tickDto.AskPrice {
		tickDto.AskPrice[idx] = mathutil.RoundToFloat(ap, constants.Precision2)
	}
	for idx, bp := range tickDto.BidPrice {
		tickDto.BidPrice[idx] = mathutil.RoundToFloat(bp, constants.Precision2)
	}

	wrapper := TickWrapper{
		StockCode: stockCode,
		LastPrice: tickDto.LastPrice,
	}
	constants.App.Event.Emit("monitor_log", wrapper)
	// data, err := json.Marshal(&tickDto)
	// if err != nil {
	// 	logrus.WithField("方法", "marketHandler.Tick").Errorf("dto转json错误: %v", err)
	// 	response.FailureWithBody(ctx, "dto转json错误", err.Error())
	// 	return
	// }

	// rdb := db.GetRedisTick()
	// rdb.ZAdd(ctx, constants.TickKeyPrefix+stockCode, redis.Z{Score: float64(tickDto.Time), Member: string(data)})
	// rdb.Expire(ctx, constants.TickKeyPrefix+stockCode, time.Duration(2)*time.Hour)

	response.Success(ctx, "Tick数据保存成功")
}
